package com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.economicLossDE.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.base.response.ResponseCode;
import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.casualtiesDE.entity.Statistics;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.economicLossDE.entity.EconomicLossED;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.economicLossDE.entity.EconomicLossEDCalculation;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.economicLossDE.param.EconomicLossParam;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.economicLossDE.service.EconomicLossEDService;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.param.AssessParam;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import com.css.fxfzfxqh.util.ResponseCreator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/06/09
 * @Description: 确定评估_经济损失
 */
@Slf4j
@RestController
@RequestMapping("/EconomicLossED")
public class EconomicLossEDController {

    @Resource
    private EconomicLossEDService economicLossEDService;

    /**
     * 新增评估
     *
     * @param param 参数
     * @return
     */
    @PostMapping("/addAssess")
    public RestResponse addAssess(@RequestBody EconomicLossED param) throws Exception {
         economicLossEDService.addAssess(param);
         return RestResponse.succeed("新增成功");
    }

    /**
     * 查看
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(String id) {
        RestResponse response = null;
        try {
            EconomicLossED vehManage=economicLossEDService.findById(id);
            response = RestResponse.succeed(vehManage);
        } catch (Exception e) {
            String errorMessage = "查看失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 修改评估
     *
     * @param param 参数
     * @return
     */
    @PostMapping("/upDateAssess")
    public RestResponse upDateAssess(@RequestBody EconomicLossED param) {
        try {
            economicLossEDService.upDateAssess(param);
            return RestResponse.succeed("修改成功");
        } catch (Exception e) {
            log.error("修改评估失败!", e);
            return RestResponse.fail("修改失败!");
        }
    }

    /**
     * 获取评估code
     */
    @PostMapping("/getCode")
    public RestResponse getCode(@RequestBody EconomicLossED param) {
        try {
            String code = economicLossEDService.getCode(param);
            return RestResponse.succeed(code);
        } catch (Exception e) {
            String errormessage = "生成code失败";
            log.error(errormessage, e);
            return RestResponse.fail("获取失败");
        }

    }

    /**
     * 分页列表
     *
     * @param param 参数
     * @return page
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request, AssessParam param) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = economicLossEDService.getPage(curPage, pageSize, param);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 计算
     *
     * @param map 参数
     * @return
     */
    @PostMapping("/calculation")
    public RestResponse calculation(@RequestBody Map<String,Object> map) {
            try{
                economicLossEDService.calculation(map);
                return RestResponse.succeed("评估完成");
            } catch (Exception e){
            return ResponseCreator.createFailResponse(e.getMessage(), ResponseCode.EVALUATE_ERROR);
        }
    }

    /**
     * 导入
     *
     * @param type 计算的类型 1省市县烈度2省市县概率,3坐标烈度,4坐标概率
     * @param id   评估记录id
     * @return
     */
    @PostMapping("/importExcel")
    public RestResponse reportImport(HttpServletRequest req, String type, String id,String code) {
        RestResponse response = null;
        try {
            //获取上传到服务器的临时文件对象
            Part part = req.getPart("file");
            //获取临时文件的输入流
            InputStream inputStream = part.getInputStream();
            String str = economicLossEDService.reportImport(inputStream, type, id,code);
            response = RestResponse.succeed(str);
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /**
     * 导出
     *
     * @param param 参数
     */
    @PostMapping("/exportData")
    public void exportData(HttpServletResponse response, EconomicLossParam param) {
        economicLossEDService.exportData(response, param);
    }

    /**
     * 查询计算值
     *
     * @param seismicInputMode 震输入方式0烈度,1四个概率水准,2地震影响场
     * @param id               评估记录id
     * @param condition        条件(地震烈度 或 四个概率水准)
     * @return
     */
    @GetMapping("/queryCalculation")
    public RestResponse queryCalculation(HttpServletRequest request, String seismicInputMode, String id,
                                         String condition) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = economicLossEDService.queryCalculation(curPage, pageSize, seismicInputMode, id, condition);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改经验评估与修正值
     *
     * @return
     */
    @PostMapping("/updateCorrectionValue")
    public RestResponse updateCorrectionValue(@RequestBody EconomicLossEDCalculation param) {
        RestResponse restResponse = null;
        try {
            economicLossEDService.updateCorrectionValue(param);
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }
    /**
     * 查询计算值
     *
     * @param seismicInputMode 地震输入方式0烈度,1四个概率水准,2地震影响场
     * @param taskId               评估记录id
     * @param condition        条件(地震烈度 或 四个概率水准)
     * @return
     */
    @GetMapping("/statistics")
    public RestResponse statistics(String seismicInputMode, String taskId, String condition) throws Exception{
        List<Statistics> list = economicLossEDService.statistics(seismicInputMode,taskId,condition);
        return ResponseCreator.createSuccessResponse(list);
    }

}
